package com.xiaochong.bitong.job.schedule;

import com.alibaba.fastjson.JSON;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.bitong.job.config.ExecutorConfig;
import com.xiaochong.bitong.job.service.ConceptService;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.mapper.ConceptCategoryMapper;
import com.xiaochong.tir.common.data.po.ConceptCategory;
import com.xiaochong.tir.common.data.vo.GaiNianZhiShuVo;
import com.xiaochong.tir.common.exception.hbase.HbaseCloseExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseConnectionExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseGetTableExcetion;
import com.xiaochong.tir.common.util.RedisKeyApp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Project : xc-tir
 * @Package Name : com.xiaochong.bitong.job.schedule
 * @Description : TODO
 * @Author : daizhiqing@xiaochong.com
 * @Creation Date : 2018年08月13日下午7:44
 * @ModificationHistory Who When What
 */
@Component
public class ConceptScheduled {

    @Autowired
    RedisServer redisServer;

    @Autowired
    ConceptService conceptService;

    @Autowired
    ConceptCategoryMapper conceptCategoryMapper;

    /**
     *  概念行情指数一年数据图表（每天零点一条）
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 38 0 ? * *")
    public void GaiNianZhiShuVoYear(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("GaiNianZhiShuVoYear" , "GaiNianZhiShuVoYear开始" , System.currentTimeMillis()+"");

            ConceptCategory conceptCategory = new ConceptCategory();
            conceptCategory.setEnableFlag( true );
            conceptCategory.setDeleteFlag( false );
            List<ConceptCategory> conceptCategoryList = conceptCategoryMapper.selectByEntityList( conceptCategory );
            for(int i=0;i<conceptCategoryList.size();i++){
                List list=new ArrayList();
                conceptCategory = conceptCategoryList.get( i );
                GaiNianZhiShuVo gaiNianZhiShuVo = conceptService.getConceptualQuotesOfYear(conceptCategory.getId());
                list.add( gaiNianZhiShuVo );
                if(gaiNianZhiShuVo != null){
                    redisServer.set( RedisKeyApp.BOTTOMTEZHI_INDEX+365+":"+24+":"+conceptCategory.getId(),JSON.toJSONString( list ));
                }
            }
            LogTrace.info("GaiNianZhiShuVoYear" , "GaiNianZhiShuVoYear结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }

    /**
     *  概念行情指数30天数据图表（每4小时一条）
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 12 0/4 * * ?")
    public void GaiNianZhiShuVo30Days(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("GaiNianZhiShuVo30Days" , "GaiNianZhiShuVo30Days开始" , System.currentTimeMillis()+"");

            ConceptCategory conceptCategory = new ConceptCategory();
            conceptCategory.setEnableFlag( true );
            conceptCategory.setDeleteFlag( false );
            List<ConceptCategory> conceptCategoryList = conceptCategoryMapper.selectByEntityList( conceptCategory );
            for(int i=0;i<conceptCategoryList.size();i++){
                List list=new ArrayList();
                conceptCategory = conceptCategoryList.get( i );
                GaiNianZhiShuVo gaiNianZhiShuVo = conceptService.getConceptualQuotesOf30Days(conceptCategory.getId());
                list.add( gaiNianZhiShuVo );
                if(gaiNianZhiShuVo != null){
                    redisServer.set( RedisKeyApp.BOTTOMTEZHI_INDEX+30+":"+24+":"+conceptCategory.getId(),JSON.toJSONString( list ));
                }
            }
            LogTrace.info("GaiNianZhiShuVo30Days" , "GaiNianZhiShuVo30Days结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }


    /**
     *  概念行情指数7天数据图表（每小时一条）
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 8 0/1 * * ?")
    public void getConceptualQuotesOf7Days(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("getConceptualQuotesOf7Days" , "getConceptualQuotesOf7Days开始" , System.currentTimeMillis()+"");

            ConceptCategory conceptCategory = new ConceptCategory();
            conceptCategory.setEnableFlag( true );
            conceptCategory.setDeleteFlag( false );
            List<ConceptCategory> conceptCategoryList = conceptCategoryMapper.selectByEntityList( conceptCategory );
            for(int i=0;i<conceptCategoryList.size();i++){
                List list=new ArrayList();
                conceptCategory = conceptCategoryList.get( i );
                GaiNianZhiShuVo gaiNianZhiShuVo = conceptService.getConceptualQuotesOf7Days(conceptCategory.getId());
                list.add( gaiNianZhiShuVo );
                if(gaiNianZhiShuVo != null){
                    redisServer.set( RedisKeyApp.BOTTOMTEZHI_INDEX+7+":"+24+":"+conceptCategory.getId(),JSON.toJSONString( list ));
                }
            }
            LogTrace.info("getConceptualQuotesOf7Days" , "getConceptualQuotesOf7Days结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }


    /**
     *  概念行情指数1天数据图表（每10分钟一条）
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 */10 * * * ?")
    public void getConceptualQuotesOfOneDays(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("getConceptualQuotesOfOneDays" , "getConceptualQuotesOfOneDays开始" , System.currentTimeMillis()+"");

            ConceptCategory conceptCategory = new ConceptCategory();
            conceptCategory.setEnableFlag( true );
            conceptCategory.setDeleteFlag( false );
            List<ConceptCategory> conceptCategoryList = conceptCategoryMapper.selectByEntityList( conceptCategory );
            for(int i=0;i<conceptCategoryList.size();i++){
                List list=new ArrayList();
                conceptCategory = conceptCategoryList.get( i );
                GaiNianZhiShuVo gaiNianZhiShuVo = conceptService.getConceptualQuotesOfOneDays(conceptCategory.getId());
                list.add( gaiNianZhiShuVo );
                if(gaiNianZhiShuVo != null){
                    redisServer.set( RedisKeyApp.BOTTOMTEZHI_INDEX+1+":"+24+":"+conceptCategory.getId(),JSON.toJSONString( list ));
                }
            }
            LogTrace.info("getConceptualQuotesOfOneDays" , "getConceptualQuotesOfOneDays结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }

    /**
     *  概念行情指数3小时数据图表（每5分钟一条）
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 */5 * * * ?")
    public void getConceptualQuotesOf3Hours(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("getConceptualQuotesOf3Hours" , "getConceptualQuotesOf3Hours开始" , System.currentTimeMillis()+"");

            ConceptCategory conceptCategory = new ConceptCategory();
            conceptCategory.setEnableFlag( true );
            conceptCategory.setDeleteFlag( false );
            List<ConceptCategory> conceptCategoryList = conceptCategoryMapper.selectByEntityList( conceptCategory );
            for(int i=0;i<conceptCategoryList.size();i++){
                List list=new ArrayList();
                conceptCategory = conceptCategoryList.get( i );
                GaiNianZhiShuVo gaiNianZhiShuVo = conceptService.getConceptualQuotesOf3Hours(conceptCategory.getId());
                list.add( gaiNianZhiShuVo );
                if(gaiNianZhiShuVo != null){
                    redisServer.set( RedisKeyApp.BOTTOMTEZHI_INDEX+1+":"+3+":"+conceptCategory.getId(),JSON.toJSONString( list ));
                }
            }
            LogTrace.info("getConceptualQuotesOf3Hours" , "getConceptualQuotesOf3Hours结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }
}
